﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Linq;
using System.Text;
using System.Threading;

namespace RabbitMQSendReceivedLocalhostConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = GetConnectionFactory();
            var channelQueueName = "BrukeQueue";

            while (true)
            {
                Console.WriteLine("RabbitMQSendReceivedLocalhostConsole！");
                Console.WriteLine("输入：1-发送端 2-消费端 3-退出");
                var input = Console.ReadLine();
                if (input == "1")
                {
                    while (true)
                    {
                        Console.WriteLine("已进入：发送端,请输入消息。(输入3跳出)");
                        input = Console.ReadLine();
                        if (input == "3")
                        {
                            break;
                        }
                        SendMQ(input, factory, channelQueueName);
                    }
                }
                if (input == "2")
                {
                    Console.WriteLine("已进入：消费端。(输入3跳出)");
                    ReceivedMQ(factory, channelQueueName);
                }
                if (input == "3")
                {
                    break;
                }

                else
                {
                    Console.WriteLine("输入有误");
                    continue;
                }

            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="factory"></param>
        /// <param name="channelQueueName"></param>
        private static void SendMQ(string message, ConnectionFactory factory, string channelQueueName)
        {
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(channelQueueName, false, false, false, null);//定义队列
                    var properties = channel.CreateBasicProperties();//属性
                    properties.DeliveryMode = 1;//非持久性
                    channel.BasicPublish("", channelQueueName, properties, Encoding.UTF8.GetBytes(message));//发送消息
                }
            }

        }

        /// <summary>
        /// 消费消息
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="channelQueueName"></param>
        private static void ReceivedMQ(ConnectionFactory factory, string channelQueueName)
        {

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(channelQueueName, false, false, false, null);//定义队列
                                                                                      //创建消费者
                    var consumer = new EventingBasicConsumer(channel);//消费者
                    channel.BasicConsume(channelQueueName, true, consumer);//消费消息
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body.ToArray());
                        Console.WriteLine($"接收消息：{message}", ConsoleColor.DarkBlue);
                    };
                    Console.ReadLine();
                }
            }

        }

        private static ConnectionFactory GetConnectionFactory()
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";//85-->5672
            factory.UserName = "admin";
            factory.Password = "admin";
            factory.VirtualHost = "my_vhost";
            factory.Endpoint = new AmqpTcpEndpoint("localhost", 5672);
            return factory;


            //远程+端口配置化
            //var factory = new ConnectionFactory();
            //factory.HostName = "47.112.201.220";//85-->5672
            //factory.UserName = "bruke";
            //factory.Password = "123456";
            //factory.VirtualHost = "my_vhost";
            //factory.Endpoint = new AmqpTcpEndpoint("47.112.201.220", 85);//因为远程的服务器端口是85，不是默认5672

            //默认
            //var factory = new ConnectionFactory();
            //factory.HostName = "localhost";//主机名，Rabbit会拿这个IP生成一个endpoint，这个很熟悉吧，就是socket绑定的那个终结点。
            //factory.UserName = "admin";//默认用户名,用户可以在服务端自定义创建，有相关命令行
            //factory.Password = "admin";//默认密码
        }

    }
}
